﻿using DailyPoetryM.Services;
using DailyPoetryM.ViewModels;

namespace DailyPoetryM;
public class ServiceLocator
{
    private IServiceProvider _serviceProvider;
    public ResultPageViewModel ResultPageViewModel =>
        _serviceProvider.GetService<ResultPageViewModel>();//叫依赖注入，IoC

    public ServiceLocator()
    {
        //先将服务放到一个服务器容器中，方便通过依赖注入的方式获取
        var serviceCollection = new ServiceCollection();

        //注册服务
        serviceCollection.AddSingleton<ResultPageViewModel>();
        //注册接口和实例,这样注册，再需要接口实例时就会给一个实例对象
        serviceCollection.AddSingleton<IPoetryStorage, PoetryStorage>();
        serviceCollection.AddSingleton<IPreferenceStorage, PreferenceStorage>();

        _serviceProvider = serviceCollection.BuildServiceProvider();
    }
}
